<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="小名" prop="petName">
            <el-input v-model="queryParams.petName" placeholder="请输入小名" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input v-model="queryParams.age" placeholder="请输入年龄" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="是否有疾病" prop="isIll" label-width="150px">
            <el-select v-model="queryParams.isIll" placeholder="请选择是否有疾病" clearable>
              <el-option v-for="dict in whether_or_not" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="家庭是否有低保" prop="isSubsistenceAllowance" label-width="150px">
            <el-select v-model="queryParams.isSubsistenceAllowance" placeholder="请选择家庭是否有低保" clearable>
              <el-option v-for="dict in whether_or_not" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="家庭是否是精准扶贫户" prop="isPreciseHelp" label-width="155px">
            <el-select v-model="queryParams.isPreciseHelp" placeholder="请选择家庭是否有低保" clearable>
              <el-option v-for="dict in whether_or_not" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['business:orphan:add']">新增 </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['business:orphan:edit']"
              >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['business:orphan:remove']"
              >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['business:orphan:export']">导出 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="orphanList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!--        <el-table-column label="用户ID" align="center" prop="userId" />-->
        <el-table-column label="图片" align="center" prop="picture" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.pictureUrl" :width="80" :height="80" />
          </template>
        </el-table-column>
        <!--        <el-table-column label="图片" align="center" prop="picture" />-->
        <el-table-column label="姓名" align="center" prop="name"  width="200px" show-overflow-tooltip/>
        <el-table-column label="小名" align="center" prop="petName"  width="200px" show-overflow-tooltip/>
        <el-table-column label="出生日期" align="center" prop="birthDate"  width="200px" show-overflow-tooltip/>
        <el-table-column label="年龄" align="center" prop="age"  width="200px" show-overflow-tooltip/>
        <el-table-column label="地区" align="center" prop="area"  width="200px" show-overflow-tooltip/>
        <el-table-column label="街道" align="center" prop="street"  width="200px" show-overflow-tooltip/>
        <el-table-column label="社区" align="center" prop="community"  width="200px" show-overflow-tooltip/>
        <el-table-column label="民族" align="center" prop="nationality"  width="200px" show-overflow-tooltip/>
        <el-table-column label="身份证号" align="center" prop="idCard"  width="200px" show-overflow-tooltip/>
        <el-table-column label="身高" align="center" prop="height"  width="200px" show-overflow-tooltip/>
        <el-table-column label="体重" align="center" prop="weight"  width="200px" show-overflow-tooltip/>
        <el-table-column label="是否有疾病" align="center" prop="isIll"  width="200px" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="whether_or_not" :value="scope.row.isIll" />
          </template>
        </el-table-column>
        <el-table-column label="疾病" align="center" prop="ill"  width="200px" show-overflow-tooltip/>
        <el-table-column label="监护人姓名" align="center" prop="guardianName"   width="200px" show-overflow-tooltip/>
        <el-table-column label="联系方式" align="center" prop="guardianTel"  width="200px" show-overflow-tooltip/>
        <el-table-column label="与孩子关系" align="center" prop="relation"  width="200px" show-overflow-tooltip />
        <el-table-column label="邻居姓名" align="center" prop="neighborName"  width="200px" show-overflow-tooltip/>
        <el-table-column label="联系方式" align="center" prop="neighborTel"  width="200px" show-overflow-tooltip/>
        <el-table-column label="村(居)委书记姓名" align="center" prop="villageName"  width="200px" show-overflow-tooltip />
        <el-table-column label="村(居)委书记联系方式" align="center" prop="villageTel"   width="200px" show-overflow-tooltip />
        <el-table-column label="家庭主要联系人姓名" align="center" prop="primaryName"   width="200px" show-overflow-tooltip />
        <el-table-column label="家庭主要联系人联系方式" align="center" prop="primaryTel"   width="200px" show-overflow-tooltip />
        <el-table-column label="学校" align="center" prop="school" />
        <el-table-column label="班主任姓名" align="center" prop="teacherName"  w width="200px" show-overflow-tooltip/>
        <el-table-column label="班主任联系方式" align="center" prop="teacherTel"   width="200px" show-overflow-tooltip/>
        <el-table-column label="家庭情况" align="center" prop="familyInfo" width="200px" show-overflow-tooltip />
        <el-table-column label="家庭是否有低保" align="center" prop="isSubsistenceAllowance" width="100px">
          <template #default="scope">
            <dict-tag :options="whether_or_not" :value="scope.row.isSubsistenceAllowance" />
          </template>
        </el-table-column>
        <el-table-column label="家庭是否是精准扶贫户" align="center" prop="isPreciseHelp" width="155px">
          <template #default="scope">
            <dict-tag :options="whether_or_not" :value="scope.row.isPreciseHelp" />
          </template>
        </el-table-column>
        <el-table-column label="微心愿" align="center" prop="wish" />
        <el-table-column label="孩子现状" align="center" prop="helpType"  />
        <el-table-column label="帮扶需求" align="center" prop="helpNeeds"  />

        <el-table-column label="操作" align="center" fixed="right">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:orphan:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:orphan:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改孤儿详细信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="orphanFormRef" :model="form" :rules="rules" label-width="80px" label-position="top">
        <!--        <el-form-item label="用户ID" prop="userId">-->
        <!--          <el-input v-model="form.userId" placeholder="请输入用户ID" />-->
        <!--        </el-form-item>-->

        <el-form-item label="图片" prop="picture" label-width="150px">
          <image-upload v-model="form.picture" />
        </el-form-item>
        <el-form-item label="姓名" prop="name" label-width="150px">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="小名" prop="petName" label-width="150px">
          <el-input v-model="form.petName" placeholder="请输入小名" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable
                          v-model="form.birthDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="年龄" prop="age" label-width="150px">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="地区" prop="area" label-width="150px">
          <el-input v-model="form.area" placeholder="请输入地区" />
        </el-form-item>
        <el-form-item label="街道" prop="street" label-width="150px">
          <el-input v-model="form.street" placeholder="请输入街道" />
        </el-form-item>
        <el-form-item label="社区" prop="community" label-width="150px">
          <el-input v-model="form.community" placeholder="请输入社区" />
        </el-form-item>
        <el-form-item label="民族" prop="nationality" label-width="150px">
          <el-input v-model="form.nationality" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard" label-width="150px">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="身高" prop="height" label-width="150px">
          <el-input v-model="form.height" placeholder="请输入身高" />
        </el-form-item>
        <el-form-item label="体重" prop="weight" label-width="150px">
          <el-input v-model="form.weight" placeholder="请输入体重" />
        </el-form-item>
        <el-form-item label="是否有疾病" prop="isIll" label-width="150px">
          <el-select v-model="form.isIll" placeholder="请选择是否有疾病">
            <el-option v-for="dict in whether_or_not" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="疾病" prop="ill" label-width="150px">
          <el-input v-model="form.ill" placeholder="请输入疾病" />
        </el-form-item>
        <el-form-item label="监护人姓名" prop="guardianName" label-width="150px">
          <el-input v-model="form.guardianName" placeholder="请输入监护人姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="guardianTel" label-width="150px">
          <el-input v-model="form.guardianTel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="与孩子关系" prop="relation" label-width="150px">
          <el-input v-model="form.relation" placeholder="请输入与孩子关系" />
        </el-form-item>
        <el-form-item label="邻居姓名" prop="neighborName" label-width="150px">
          <el-input v-model="form.neighborName" placeholder="请输入邻居姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="neighborTel" label-width="150px">
          <el-input v-model="form.neighborTel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="村(居)委书记姓名" prop="villageName" label-width="150px">
          <el-input v-model="form.villageName" placeholder="请输入村(居)委书记姓名" />
        </el-form-item>
        <el-form-item label="村(居)委书记联系方式" prop="villageTel" label-width="150px">
          <el-input v-model="form.villageTel" placeholder="请输入村(居)委书记联系方式" />
        </el-form-item>
        <el-form-item label="家庭主要联系人姓名" prop="primaryName" label-width="150px">
          <el-input v-model="form.primaryName" placeholder="请输入家庭主要联系人姓名" />
        </el-form-item>
        <el-form-item label="家庭主要联系人联系方式" prop="primaryTel" label-width="150px">
          <el-input v-model="form.primaryTel" placeholder="请输入家庭主要联系人联系方式" />
        </el-form-item>
        <el-form-item label="学校" prop="school" label-width="150px">
          <el-input v-model="form.school" placeholder="请输入学校" />
        </el-form-item>
        <el-form-item label="班主任姓名" prop="teacherName" label-width="150px">
          <el-input v-model="form.teacherName" placeholder="请输入班主任姓名" />
        </el-form-item>
        <el-form-item label="班主任联系方式" prop="teacherTel" label-width="150px">
          <el-input v-model="form.teacherTel" placeholder="请输入班主任联系方式" />
        </el-form-item>
        <el-form-item label="家庭情况" prop="familyInfo" label-width="150px">
          <el-input v-model="form.familyInfo" placeholder="请输入家庭情况"  />
        </el-form-item>
        <el-form-item label="家庭是否有低保" prop="isSubsistenceAllowance"label-width="150px">
          <el-select v-model="form.isSubsistenceAllowance" placeholder="请选择家庭是否有低保">
            <el-option v-for="dict in whether_or_not" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家庭是否是精准扶贫户" prop="isPreciseHelp" label-width="150px">
          <el-select v-model="form.isPreciseHelp" placeholder="请输入家庭是否是精准扶贫户">
            <el-option v-for="dict in whether_or_not" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="微心愿" prop="wish" label-width="150px">
          <el-input v-model="form.wish" placeholder="请输入微心愿" />
        </el-form-item>
        <el-form-item label="孩子现状" prop="helpType" label-width="150px">
          <el-select v-model="form.helpType" placeholder="请选择帮孩子现状">
            <el-option
              v-for="dict in helpTypeList"
              :key="dict.configName"
              :label="dict.configName"
              :value="dict.configName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮扶需求" prop="helpNeedsF" label-width="150px">
          <el-select v-model="form.helpNeedsF" multiple placeholder="请选择帮扶需求">
            <el-option
              v-for="dict in helpNeedsList"
              :key="dict.configName"
              :label="dict.configName"
              :value="dict.configName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in business_information_zt"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Orphan" lang="ts">
import { listOrphan, getOrphan, delOrphan, addOrphan, updateOrphan, add } from '@/api/business/orphan';
import { OrphanVO, OrphanQuery, OrphanForm } from '@/api/business/orphan/types';
import { getByKeyApi } from '@/api/business/config';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { whether_or_not,business_information_zt } = toRefs<any>(proxy?.useDict('whether_or_not','business_information_zt'));

const orphanList = ref<OrphanVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const orphanFormRef = ref<ElFormInstance>();
const helpNeedsList = ref([]);
const helpTypeList = ref([]);

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: OrphanForm = {
  userId: undefined,
  picture: undefined,
  name: undefined,
  petName: undefined,
  birthDate: undefined,
  age: undefined,
  area: undefined,
  street: undefined,
  community: undefined,
  nationality: undefined,
  idCard: undefined,
  height: undefined,
  weight: undefined,
  isIll: undefined,
  ill: undefined,
  guardianName: undefined,
  guardianTel: undefined,
  relation: undefined,
  neighborName: undefined,
  neighborTel: undefined,
  villageName: undefined,
  villageTel: undefined,
  primaryName: undefined,
  primaryTel: undefined,
  school: undefined,
  teacherName: undefined,
  teacherTel: undefined,
  familyInfo: undefined,
  isSubsistenceAllowance: undefined,
  isPreciseHelp: undefined,
  wish: undefined,
  helpType: undefined,
  helpNeeds: undefined,
  helpNeedsF: undefined
};
const data = reactive<PageData<OrphanForm, OrphanQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    petName: undefined,
    age: undefined,
    isIll: undefined,
    isSubsistenceAllowance: undefined,
    isPreciseHelp: undefined,
    params: {}
  },
  rules: {
    userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
    // picture: [
    //   { required: true, message: "图片不能为空", trigger: "blur" }
    // ],
    name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
    petName: [{ required: true, message: '小名不能为空', trigger: 'blur' }],
    // birthDate: [{ required: true, message: '出生日期不能为空', trigger: 'blur' }],
    age: [{ required: true, message: '年龄不能为空', trigger: 'blur' }],
    area: [{ required: true, message: '地区不能为空', trigger: 'blur' }],
    street: [{ required: true, message: '街道不能为空', trigger: 'blur' }],
    community: [{ required: true, message: '社区不能为空', trigger: 'blur' }],
    // nationality: [
    //   { required: true, message: "民族不能为空", trigger: "blur" }
    // ],
    // idCard: [
    //   { required: true, message: "身份证号不能为空", trigger: "blur" }
    // ],
    // height: [
    //   { required: true, message: "身高不能为空", trigger: "blur" }
    // ],
    // weight: [
    //   { required: true, message: "体重不能为空", trigger: "blur" }
    // ],
    // isIll: [
    //   { required: true, message: "是否有疾病不能为空", trigger: "change" }
    // ],
    // ill: [
    //   { required: true, message: "疾病不能为空", trigger: "blur" }
    // ],
    // guardianName: [{ required: true, message: '监护人姓名不能为空', trigger: 'blur' }],
    // guardianTel: [{ required: true, message: '联系方式不能为空', trigger: 'blur' }],
    // relation: [{ required: true, message: '与孩子关系不能为空', trigger: 'blur' }],
    // neighborName: [
    //   { required: true, message: "邻居姓名不能为空", trigger: "blur" }
    // ],
    // neighborTel: [
    //   { required: true, message: "联系方式不能为空", trigger: "blur" }
    // ],
    // villageName: [
    //   { required: true, message: "村(居)委书记姓名不能为空", trigger: "blur" }
    // ],
    // villageTel: [
    //   { required: true, message: "村(居)委书记联系方式不能为空", trigger: "blur" }
    // ],
    // primaryName: [
    //   { required: true, message: "家庭主要联系人姓名不能为空", trigger: "blur" }
    // ],
    // primaryTel: [
    //   { required: true, message: "家庭主要联系人联系方式不能为空", trigger: "blur" }
    // ],
    // school: [
    //   { required: true, message: "学校不能为空", trigger: "blur" }
    // ],
    // teacherName: [
    //   { required: true, message: "班主任姓名不能为空", trigger: "blur" }
    // ],
    // teacherTel: [
    //   { required: true, message: "班主任联系方式不能为空", trigger: "blur" }
    // ],
    familyInfo: [{ required: true, message: '家庭情况不能为空', trigger: 'blur' }],
    // isSubsistenceAllowance: [
    //   { required: true, message: "家庭是否有低保不能为空", trigger: "change" }
    // ],
    // isPreciseHelp: [
    //   { required: true, message: "家庭是否是精准扶贫户不能为空", trigger: "blur" }
    // ],
    wish: [{ required: true, message: '微心愿不能为空', trigger: 'blur' }],
    helpType: [{ required: true, message: '孩子现状不能为空', trigger: 'change' }],
    helpNeeds: [{ required: true, message: '帮扶需求不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

const getByKey = async () => {
  const helpNeeds = await getByKeyApi('helpNeeds');
  helpNeedsList.value = helpNeeds.data;
  const helpType = await getByKeyApi('helpType');
  helpTypeList.value = helpType.data;
};

/** 查询孤儿详细信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listOrphan(queryParams.value);
  orphanList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  orphanFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: OrphanVO[]) => {
  ids.value = selection.map((item) => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加孤儿详细信息';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: OrphanVO) => {
  reset();
  const _userId = row?.userId || ids.value[0];
  const res = await getOrphan(_userId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改孤儿详细信息';
};

/** 提交按钮 */
const submitForm = () => {
  orphanFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      console.log("form.value", form.value)
      var join = form.value.helpNeedsF.join(",");
      form.value.helpNeeds=join
      buttonLoading.value = true;
      if (form.value.userId) {
        await updateOrphan(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await add(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('修改成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: OrphanVO) => {
  const _userIds = row?.userId || ids.value;
  await proxy?.$modal.confirm('是否确认删除孤儿详细信息编号为"' + _userIds + '"的数据项？').finally(() => (loading.value = false));
  await delOrphan(_userIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'business/orphan/export',
    {
      ...queryParams.value
    },
    `orphan_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
  getByKey();
});
</script>
